package cc.web.vaaking.servlet.system;

import cc.web.vaaking.common.AjaxResult;
import cc.web.vaaking.common.AjaxTool;
import cc.web.vaaking.entity.SysUser;
import cc.web.vaaking.services.SysUserService;
import cc.web.vaaking.servlet.BaseHttpServlet;
import com.github.pagehelper.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/test/*")
public class TestServlet extends BaseHttpServlet {

    SysUserService sysUserService = new SysUserService();

    @Override
    public void init() throws ServletException {
        routerMap.put("test",this::test);
        routerMap.put("list",this::list);
        routerMap.put("index",this::index);
    }


    private void index(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getRequestDispatcher("/views/system/list.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void list(HttpServletRequest request, HttpServletResponse response) {
        PageInfo<SysUser> pageInfo=new PageInfo<>(sysUserService.selectAllSysUser());
        AjaxTool.returnAjax(response, AjaxResult.success(pageInfo));
    }

    private void test(HttpServletRequest request, HttpServletResponse response) {
        AjaxTool.returnAjax(response, AjaxResult.success("test"));
    }
    
    
}
